#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <streambuf>
#include <cstring>
#include <cstdlib>
#include <list>

//转换
using namespace std;

string Int2StrDec(uint32_t num)
{
	stringstream _z;
	string ret;
	_z << num;
	try {
		_z >> ret;
	}
	catch(...) {

	}
	return ret;
}

class CppCgiLog
{

public:
	void Add(const string msg)
	{
		ofstream fileobj;
		fileobj.open("cppcgi.log", ios::out);
		fileobj << msg << endl;
		fileobj.close();
	}
};

class Complex //复数类
{
public:
	double real;//实数
	double imag;//虚数

	Complex(double real = 0, double imag = 0)
	{
		this->real = real;
		this->imag = imag;
	}
	void AddPublic(int s)
	{
		real += 10;
	}
private:
	void AddPrivate(double s)
	{
		real += 10;
	}
};

#if 1
Complex operator+(Complex com1, Complex com2)//运算符重载函数
{
	com1.AddPublic(10);
	return Complex(com1.real + com2.real, com1.imag + com2.imag);
}
#endif

struct rkv
{
    string key;
    string value;
};

struct rfile
{
    char* key;
    char* name;
    char* type;
    char* data;
    unsigned int len;
};

class Form
{
public:
    string operator[](const string& key);
    Form(list<struct rkv>& prop):FormData(prop)
    {
        /*
        list<struct kv>::iterator it;
        for (it = FormData.begin(); it != FormData.end(); it++)
        {
            cout << (*it).key << "=>" << (*it).value << endl;
        }
        */
    }
    bool Insert(const string& key, const string& value);
private:
    list<struct rkv> FormData;
};

class FileForm
{
public:
    struct rfile operator[](const string& key);
    FileForm(list<struct rfile>& prop):FormFile(prop)
    {
        /*
        list<struct kv>::iterator it;
        for (it = FormData.begin(); it != FormData.end(); it++)
        {
            cout << (*it).key << "=>" << (*it).value << endl;
        }
        */
    }
    bool Insert(struct rfile& file);
private:
    list<struct rfile> FormFile;
};

#if 0
class test
{
public:
    char data[20];
    char& operator[](const unsigned __int64 index);
    const char& operator [](const unsigned __int64 index) const;
};
char& test::operator [](const unsigned __int64 index)////重载[]操作符，作为左值
{
    return data[index];
}
const char& test::operator [](const unsigned __int64 index) const////重载[]操作符，作为右值
{
    return data[index];
}
#endif

#if 1
string Form::operator[](const string& key)
{
    list<struct rkv>::iterator it;
    for (it = FormData.begin(); it != FormData.end(); it++)
    {
        if ((*it).key == key)
        {
           return (*it).value;
        }
    }
    return "";
}

struct rfile FileForm::operator[](const string& key)
{
    list<struct rfile>::iterator it;
    struct rfile file;
    for (it = FormFile.begin(); it != FormFile.end(); it++)
    {
        if ((*it).key == key)
        {
           file = (*it);
        }
    }
    return file;
}
#endif


class PropRequest
{
public:
    Form post;
    Form get;
    FileForm file;

public:
    PropRequest();
    ~PropRequest();

private:
    string method;
};
#if 0
class LogStreamBuf : public std::streambuf {
 public:
  LogStreamBuf(char *buf, int len) {
    setp(buf, buf + len - 2);
  }
};

class LogStream : public std::ostream {
  public:
    LogStream(char *buf, int len):streambuf_(buf, len)
    {
      rdbuf(&streambuf_);
    }
  private:
    LogStreamBuf streambuf_;
};
#endif


int main()
{
    Complex com1(10,10), com2(20,20), sub, sum;
    sum = com1 + com2;
	sub = operator+(com1, com2);

	//CppCgiLog log;
	//log.Add("sum = " + Int2StrDec(sum.real) + " + " + Int2StrDec(sum.imag) + "i");

    cout << "sum = " << sum.real << " + " << sum.imag << "i" << endl;

    int i;
    struct rkv a;
    list<struct rkv> prop;
    for (i = 0; i < 10; i++)
    {
        a.key = "key" + Int2StrDec(i);
        a.value = "value" + Int2StrDec(i);
        prop.push_back(a);
    }
#if 0
    for (i = 0; i < 10; i++)
    {
        a = prop.front();
        prop.pop_front();
        cout << a.key << "=>" << a.value << endl;
    }
#endif
    Form hello(prop);
    cout << hello["key7"] << endl;
	//ofstream out("cppcgi.log");
	//cout.rdbuf(out.rdbuf());
#if 0
	char buf[2000] = {0};
	LogStream* zz = new LogStream(buf, (int)buf + 2000 - 2);
	*zz << "sum = " << sum.real << " + " << sum.imag << "i";
	cout << buf << endl;
#endif
	return 0;
}
